﻿
using PiFramework;
using PiUi.View;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace PiUi.Froms
{
    public partial class ReportView : BaseView
    {
        public ReportView()
        {

            InitializeComponent();
            PNotificationCenter.DefaultCenter().AddObserver("UpdataTable", this.OnNotification);
            dataGridView1.Font = new Font("微软雅黑", 10, FontStyle.Regular);
            dataGridView1.DataSource = dt;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
         
        }
        public override void LoadTheme()
        {
            dataGridView1.GridColor = ThemeColor.SecondaryColor;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = ThemeColor.PrimaryColor;
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 13, FontStyle.Bold);
            this.labelTestData.ForeColor = ThemeColor.SecondaryColor;

            foreach (Control btns in this.Controls)
            {
                if (btns.GetType() == typeof(System.Windows.Forms.Button))
                {
                    System.Windows.Forms.Button btn = (System.Windows.Forms.Button)btns;
                    btn.BackColor = ThemeColor.PrimaryColor;
                    btn.ForeColor = Color.White;
                    btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
                }
            }
        }
        
        private void ReportView_Load(object sender, EventArgs e)
        {
        }

        DataTable dt = new DataTable();
        public void OnNotification(string name, object sender, Dictionary<string, object> userInfo)
        {
            if (this.IsHandleCreated)
            {
                Invoke(new Action(() =>
                {
                    if (name == "UpdataTable")
                    {
                        try
                        {

                            Console.WriteLine("更新表格");
                            // 关键代码：将字典数据追加到DataTable
                            dt = AppendDictionaryToDataTable(dt, userInfo);
                            // 可选：触发UI更新（例如刷新DataGridView）
                            if (dataGridView1.DataSource == null)
                            {
                                dataGridView1.DataSource = dt;
                            }
                            dataGridView1.Refresh();
                        }
                        catch (Exception ex)
                        {
                            // 处理异常（例如日志记录或弹窗提示）
                            MessageBox.Show($"更新表格失败: {ex.Message}");
                        }
                    }
                }));
            }
        }

        /// <summary>
        /// 将字典追加到 DataTable（自动创建列）
        /// </summary>
        /// <param name="dataTable">目标 DataTable</param>
        /// <param name="rowData">字典数据</param>
        /// <returns>更新后的 DataTable</returns>
        private  DataTable AppendDictionaryToDataTable(DataTable dataTable, Dictionary<string, object> rowData)
        {
            // 如果 DataTable 为空，初始化
            if (dataTable == null)
            {
                dataTable = new DataTable();
            }
            // 自动添加缺失列（检查每个字典的 Key）
            foreach (var key in rowData.Keys)
            {
                if (!dataTable.Columns.Contains(key))
                {
                    // 动态创建列：列名=key，类型=值的实际类型（或 object）
                    Type columnType = rowData[key]?.GetType() ?? typeof(object);
                    dataTable.Columns.Add(key, columnType);
                }
            }

            // 添加新行
            DataRow newRow = dataTable.NewRow();
            foreach (var kvp in rowData)
            {
                newRow[kvp.Key] = kvp.Value ?? DBNull.Value;
            }
            dataTable.Rows.Add(newRow);

            return dataTable;
        }

        private void buttonExportData_Click(object sender, EventArgs e)
        {

         

        }
    }

  
      
   

}
